<?php
namespace app\index\controller;

use \app\backend\model\Menu;
use app\backend\org\HeadTitle;
use \think\Db;
use app\backend\org\Crumbs;
use app\backend\org\Kv;

/**
 * 关于领星控制器
 */
class Aboutus  extends \think\Controller
{
    //关于领星接口
    public function index()
    {
        try {
            $menu_id = input('menu_id', '-1', 'htmlspecialchars');//第三个参数过滤
            $language = input("lang", "cn", "htmlspecialchars");
            //当前页数
            $page = input('page', '1', 'htmlspecialchars');
            //每页显示条数
            $psize = input('psize', '1', 'htmlspecialchars');
            if ($language == "cn") {
                $tab_name = "tab_name_cn";
                $team_name = "team_name_cn";
                $name = "name_cn";
                $dictum = "dictum_cn";
                $content = "content_cn";
                //$content_p = "content_p_cn";
                //$content_s = "content_s_cn";
                //$content_list = "content_cn_list";
               // $duty_list = "duty_list_cn";
                $duty = "duty_cn";
                $list_title = "list_title_cn";
                $title_introduce = "title_introduce_cn";
                $department_name = "department_name_cn";
                //$stage_title = "stage_title_cn";
                $vio_intro = "vio_intro_cn";
                $vio_name = "vio_name_cn";
            } elseif ($language == "en") {
                $tab_name = "tab_name_en";
                $team_name = "team_name_en";
                $name = "name_en";
                $dictum = "dictum_en";
                $content = "content_en";
                //$content_p = "content_p_en";
                //$content_s = "content_s_en";
                //$content_list = "content_en_list";
                $duty = "duty_en";
                //$duty_list = "duty_list_en";
                $list_title = "list_title_en";
                $title_introduce = "title_introduce_en";
                $department_name = "department_name_en";
                //$stage_title = "stage_title_en";
                $vio_intro = "vio_intro_en";
                $vio_name = "vio_name_en";
            }
            $tab_name_const = "tab_name_cn";
            $data = Menu::All(["p_id" => ["exp", "<>0"]]);
            //调用headtitle查title内容
            $head_title = HeadTitle::title($data, $menu_id, $language, "menu");
//            echo "<pre>";
//             print_r($head_title);exit;
            //调用kv类查kv画面内容
            $menu = Kv::Kv($menu_id);
//            echo "<pre>";
//            print_r($menu);exit;
            $menu_content = Db::query("select $content as content,type_id from ls_other where `p_id`=" . $menu[0]['menu_id'] . " and `delete_time` is null ");

            //tab分类切换
            // $tab = Db::query("select tab_id,type_id,$tab_name from ls_tab where `p_id`={$menu[0]['menu_id']} and `delete_time` is null order by sort ASC ");
            $tab = Db::query("select b.tab_id,b.$tab_name as tab_name,b.tab_name_cn as tab_name_cn_if,b.type_id,t.tt_id from ls_tab b left join ls_tab_type t on b.tab_id=t.tab_id where b.p_id={$menu[0]['menu_id']} and b.delete_time is null order by b.sort ASC ");

            $lxp = Db::query("select tab_id from ls_tab where $tab_name_const='领星人' and `delete_time` is null");
            //var_dump($lxp[0]['tab_id']);exit;
            //tab=领星人连接people表
            $team = Db::query("select team_id,tab_id,$team_name as team_name from ls_team where `tab_id`={$lxp[0]['tab_id']} and  `delete_time` is null order by sort ASC");

            foreach ($tab as $k => $v) {
                foreach ($team as $a => $b) {
                    if ($v["tab_name_cn_if"] == "领星人") {
                        if (isset($tab[$k]["child"])) {
                            $tab[$k]["child"][] = $b;
                        } else {
                            $tab[$k]["child"] = [$b];
                        }
                    }
                }
            }
//            return json($tab);exit;
//            var_dump($team);exit;
            //团队风采
            $mien = Db::query("select d.department_id,d.$department_name as department_name,p.sort,p.people_id,p.$name as name,p.s_img_url,p.b_img_url,i.intro_id,i.$content as content from ls_people p left join ls_people_intro i on p.people_id=i.people_id left join ls_department d on d.department_id=p.department_id  where d.delete_time is null and p.delete_time is null and i.delete_time is null and p.team_id=1 order by p.sort DESC ");


            //管理团队
            $people = Db::query("select p.people_id,p.$name as name,y.duty_id,y.$duty as duty,p.$dictum as dictum,p.s_img_url,p.b_img_url,p.team_id,i.intro_id,i.$content as content from ls_people p left join ls_people_intro i on p.people_id=i.people_id left join ls_duty y on p.people_id=y.people_id where p.delete_time is null and p.team_id=2 ");

            //return json($people);exit;
            //科学顾问委员会
            $meeting = Db::query("select p.people_id,p.$name as name,y.duty_id,y.$duty as duty,p.s_img_url,p.team_id,i.intro_id,i.$content as content from ls_people p left join ls_people_intro i on p.people_id=i.people_id left join ls_duty y on p.people_id=y.people_id where p.delete_time is null and p.team_id=3 ");
            $con = [];
            $temp1 = [];// 存放 people_id
            $temp2 = [];// 存放 duty_id
            //$temp3 = [];// 存放 intro_id

            foreach ($meeting as $k => $v) {
                if (!in_array($v['people_id'], $temp1)) {
                    array_push($temp1, $v['people_id']);
                    array_push($temp2, $v['duty_id']);
                    // array_push($temp3, $v['intro_id']);

                    // $v[$content] = [$v["content"]];
                    $v["duty_list"] = [$v["duty"]];

                    // unset($v["content"]);
                    unset($v["duty"]);
                    unset($v['duty_id']);
                    //unset($v['intro_id']);

                    $con[] = $v;

                } else {
                    foreach ($con as $a => $b) {
                        if ($b["people_id"] == $v['people_id']) {
                            if (!in_array($v['duty_id'], $temp2)) {
                                $con[$a]["duty_list"][] = $v["duty"];
                                array_push($temp2, $v['duty_id']);
                            }
                        }
                    }
                }
            }

           /* $con1 = [];
            $temp3 = [];// 存放 people_id
            $temp4 = [];// 存放 duty_id
            //$temp3 = [];// 存放 intro_id

            foreach ($people as $k => $v) {
                if (!in_array($v['people_id'], $temp3)) {
                    array_push($temp3, $v['people_id']);
                    array_push($temp4, $v['duty_id']);
                    // array_push($temp3, $v['intro_id']);

                    // $v[$content] = [$v["content"]];
                    $v["duty_list"] = [$v["duty"]];

                    // unset($v["content"]);
                    unset($v["duty"]);
                    unset($v['duty_id']);
                    //unset($v['intro_id']);

                    $con1[] = $v;

                } else {
                    foreach ($con1 as $a => $b) {
                        if ($b["people_id"] == $v['people_id']) {
                            if (!in_array($v['duty_id'], $temp2)) {
                                $con1[$a]["duty_list"][] = $v["duty"];
                                array_push($temp4, $v['duty_id']);
                            }
                        }
                    }
                }
            }*/

            //return json($con);exit;
            //return json($people);exit;
            foreach ($tab as $k => $v) {
                if (isset($v["child"])) {
                    foreach ($v["child"] as $a => $b) {
                        //return json($b);exit;
                        if ($b["team_id"] == 1) {
                            $tab[$k]["child"][$a]["child"] = $mien;
                        } elseif ($b["team_id"] == 2) {
                            //if($b["team_id"]==$people["team_id"]){
                            $tab[$k]["child"][$a]["child"] = $people;
                        } elseif ($b["team_id"] == 3) {
                            $tab[$k]["child"][$a]["child"] = $con;
                        }
                    }
                }
            }

//领星伙伴，领星实力
            $lx = Db::query("select tab_id from ls_tab where $tab_name_const='领星伙伴' or $tab_name_const='领星实力' and `delete_time` is null");
            //其他tab分类下面list表的内容
            $list = Db::query("select list_id,p_id,$list_title as list_title,img_url,DATE_FORMAT(news_time,'%Y-%m-%d') as news_time from ls_list where `p_id`={$lx[0]['tab_id']} or `p_id`={$lx[1]['tab_id']}  and  `delete_time` is null order by manual_sort ASC ");
            //stage表下面内容
            $stage = Db::query("select stage_id,p_id,$content as content from ls_stage where `delete_time` is null order by `manual_sort` ASC ");
            $tempss = [];
            //循环拼接三个表内容
            foreach ($tab as $k => $v) {
                foreach ($list as $a => $b) {
                    if ($b['p_id'] == $v['tab_id'] && !in_array($b['list_id'], $tempss)) {//in_array用法，$tempss中查找$b['list_id']
                        array_push($tempss, $b['list_id']);//array_push() 函数向第一个参数的数组尾部添加一个或多个元素（入栈），然后返回新数组的长度
                    }
                    foreach ($stage as $c => $d) {
                        if ($b["list_id"] == $d["p_id"] && in_array($d['p_id'], $tempss)) {
                            if (isset($list[$a]["child"])) {
                                $list[$a]["child"][] = $d;
                            } else {
                                $list[$a]["child"] = [$d];
                            }
                        }
                    }

                    if ($v["tab_id"] == $b["p_id"]) {
                        if (isset($tab[$k]["child"])) {
                            $tab[$k]["child"][] = $list[$a];
                        } else {
                            $tab[$k]["child"] = [$list[$a]];
                        }
                    }
                }
            }
            //return json($tab);exit;

            //领星新闻

            $lxnews = Db::query("select b.tab_id,b.type_id,t.tt_id from ls_tab b left join ls_tab_type t on b.tab_id=t.tab_id where b.$tab_name_const='领星新闻' and b.delete_time is null ");
            //计算偏移量
            $offset = ($page - 1) * $psize;

            //置顶新闻
            $listtop = Db::query("select list_id,p_id,type_id,is_recommend,$list_title as list_title,$title_introduce as title_introduce,img_url,DATE_FORMAT(news_time,'%Y-%m-%d') as news_time from ls_list where `p_id`={$lxnews[0]['tab_id']} and `is_recommend`='1,{$lxnews[0]['tt_id']}' and  `delete_time` is null and `is_enable`=1 order by manual_sort ASC ");
            //var_dump($listtop);
             //exit;

            //查询出p_id=32 news_id包含32 的数据
            $list_other = Db::query("select list_id,news_id,p_id,type_id,is_recommend,$list_title as list_title,DATE_FORMAT(news_time,'%Y-%m-%d') as news_time from ls_list where ((`p_id`={$lxnews[0]['tab_id']} and `is_recommend`='0,{$lxnews[0]['tt_id']}') or (news_id LIKE '%{$lxnews[0]['tab_id']}%')) and `delete_time` is null and `is_enable`=1 order by create_time DESC limit $offset,$psize ");
            $listall = array_merge($listtop, $list_other);
            //var_dump($listall);exit;
            /*
                        //tab分类下面list表的内容
                        //之前的写法先循环本身的内容 在拼接同步过来的内容
                       $listpri = Db::query("select list_id,p_id,type_id,is_recommend,$list_title as list_title,$title_introduce as title_introduce,img_url,DATE_FORMAT(news_time,'%Y-%m-%d') as news_time from ls_list where `p_id`={$lxnews[0]['tab_id']} and `is_recommend`='0,{$lxnews[0]['tt_id']}' and `delete_time` is null and `is_enable`=1 order by manual_sort ASC limit $offset,$psize");
                       // var_dump($listpri);exit;
                        $list2 = array_merge($listtop,$listpri);

                        //计算非置顶p_id= tab_id 的list的条数是否小于每页显示条数
                        if(count($listpri)<$psize){
                            //echo 111;exit;
                            //计算总页数
                            $count = Db::query("select count(*) as total from ls_list where `p_id`={$lxnews[0]['tab_id']} and `is_recommend`='0,{$lxnews[0]['tt_id']}' and `delete_time` is null and `is_enable`=1");

                            //先显示领星新闻直接新闻再显示news_id是领星新闻的
                            //领星新闻一共显示多少页
                            $offset2= (floor(($page*$psize - $count[0]["total"]) / $psize) >= 1) ? (($page*$psize - $count[0]["total"]) % $psize + ($page - ceil($count[0]["total"] / $psize) - 1) * $psize) : 0;
                            $psize2 = (floor(($page*$psize - $count[0]["total"]) / $psize) >= 1) ? $psize : (($page*$psize - $count[0]["total"]) % $psize);

                            //list表全部内容 帅选出news_id=32的 ，就是复选框选中应用过来的新闻
                            $list1 = Db::query("select list_id,news_id,p_id,type_id,is_recommend,$list_title as list_title,DATE_FORMAT(news_time,'%Y-%m-%d') as news_time,DATE_FORMAT(create_time,'%Y-%m-%d') as create_time from ls_list where news_id LIKE '%{$lxnews[0]['tab_id']}%' and `delete_time` is null and `is_enable`=1 order by manual_sort ASC limit $offset2,$psize2 ");
                //var_dump($list1);exit;
                            //添加new_id里面的新闻
                            if(!empty($list1)){
                                $listall =  array_merge($list2,$list1);
                                //var_dump($listall);exit;
                            }elseif(!empty($list2)){
                                $listall = $list2;
                            }else{
                                $listall = $list1;
                            }
            }
            */

            //var_dump($listall);exit;
            //tab下的视频表
            $voice = Db::query("select vioce_id,tab_id,type_id,$vio_name as vio_name,$vio_intro as vio_intro,img_url,vioce_url,DATE_FORMAT(news_time,'%Y-%m-%d') as news_time from ls_voice where `tab_id` = {$lxnews[0]['tab_id']} and `delete_time`is null order by manual_sort ASC");

           // return json($voice);exit;

            //循环拼接list表内容
            foreach ($tab as $k=>$v){
                foreach ($listall as $a=>$b){
                    $news_id = [];
                    if(isset($b["news_id"])){
                        $news_id =  explode('，',$b["news_id"]);
                    }
                    if($v["tab_id"]==$b["p_id"] || in_array($v["tab_id"],$news_id)){
                        if (isset($tab[$k]["child"])) {
                            $tab[$k]["child"][] = $b;
                        } else {
                            $tab[$k]["child"] = [$b];
                        }
                    }
                }
                foreach ($voice as $c=>$d){
                    if($v["tab_id"]==$d["tab_id"]){
                        if (isset($tab[$k]["child"])) {
                            $tab[$k]["child"][] = $d;
                        } else {
                            $tab[$k]["child"] = [$d];
                        }
                    }
                }
            }
//计算总页数
            $countall = Db::query("select count(*) as total from ls_list where ((`p_id`={$lxnews[0]['tab_id']} and `is_recommend`='0,{$lxnews[0]['tt_id']}') or (news_id LIKE '%{$lxnews[0]['tab_id']}%'))  and `delete_time` is null and `is_enable`=1");
            foreach ($countall as $a=>$b){
                $page_count = ceil($b['total'] / $psize);
            }
            //var_dump($page_count);exit;
            /*
             * $countp = Db::query("select count(*) as total from ls_list where `p_id`={$lxnews[0]['tab_id']} and `is_recommend`='0,{$lxnews[0]['tt_id']}' and `delete_time` is null and `is_enable`=1 ");
             * $countn = Db::query("select count(*) as total from ls_list where news_id LIKE '%{$lxnews[0]['tab_id']}%' and `delete_time` is null and `is_enable`=1");
            $countall = array_merge($countp,$countn);
            var_dump($countall);exit;
            foreach ($countall as $a=>$b){
                $page_count = ceil($b['total'] / $psize);
            }
var_dump($page_count);exit;*/
           // return json($tab);exit;

            //获取面包屑
            $crumbs =  Crumbs::crumbs($data,$menu_id,$language,"menu");
            // echo "<pre>";
            //print_r($crumbs);exit;
            //判断记录存不存在
            if( empty($menu)){
                return json([
                    'code' => 2555,
                    'message' => '不存在这个id',
                ]);
            }else{
                return json([
                    'code' => 200,
                    'head_title'=>$head_title,
                    'KV'=>$menu[0],
                    'crumbs'=>$crumbs,
                    'menu_content'=>$menu_content[0],
                    'tab'=>$tab,
                    'page_count'=>$page_count,
                    "imsg" => "关于领星-aboutus.html"
                ]);
            }
        }catch (\Exception $e){
//			$this->_log2db('index::被 catch 了');
            return json([
                'code'=>2333,
                'imsg'=>'被catch',
                'message'=>'数据库查询失败 '.$e->getMessage(),
            ]);
        }
    }


    //领星新闻接口
    public function about_news(){
        try{
            $list_id = input('list_id','-1','htmlspecialchars');//第三个参数过滤
            $language = input("lang","cn","htmlspecialchars");
            if($language=="cn"){
                $stage_title = "stage_title_cn";
                $content = "content_cn";
            }elseif($language=="en"){
                $stage_title = "stage_title_en";
                $content = "content_en";
            }

            $tab_id =  Db::query("select p_id from ls_list where `list_id`={$list_id} and `delete_time` is null");
            $menu_id = Db::query("select p_id from ls_tab where `tab_id`={$tab_id[0]["p_id"]} and `delete_time` is null ");
            //print_r($menu_id);exit;
            //调用kv类查kv画面内容
            $menu=Kv::Kv($menu_id[0]["p_id"]);

            //获取stage段落并组合
            $stage = Db::query("select stage_id,$stage_title as stage_title,$content as content,DATE_FORMAT(news_time,'%Y-%m-%d') as news_time from ls_stage where `p_id`={$list_id} and `delete_time` is null and `is_enable`=1 order by `manual_sort` ASC ");
            if(empty($stage)){
                $stage[0]="";
            }
            //echo "<pre>";
            // print_r($stage);exit;
            $data = Menu::All(["p_id"=>["exp","<>0"]]);
            //调用headtitle查title内容
            $head_title = HeadTitle::title($data,$list_id,$language,"list");

            //获取面包屑
            $crumbs =  Crumbs::crumbs($data,$list_id,$language,"list");
            //exit;
            // echo "<pre>";
            // print_r($crumbs);exit;
            // return json($crumbs);exit;
            //判断记录存不存在
            if( empty($menu)){
                return json([
                    'code' => 2555,
                    'message' => '不存在这个id',
                ]);
            }else{
                return json([
                    'code' => 200,
                    'head_title'=>$head_title,
                    'KV'=>$menu[0],
                    'crumbs'=>$crumbs,
                    'stage'=>$stage[0],
                    "imsg" => "关于领星-aboutus-news-1.html"
                ]);
            }
        }catch (\Exception $e){
//			$this->_log2db('index::被 catch 了');
            return json([
                'code'=>2333,
                'imsg'=>'被catch',
                'message'=>'数据库查询失败'.$e->getMessage(),
            ]);
        }
    }

}
?>